var detail;
var limit = 6;
$(function () {
    detail = new Vue({
        el:'#detailController',
        data:{
            isAuction:-1,
            isVerify:_verify,
            verifyAuth:0,
            item:{},
            auction:{},
            imgs:[],
            keywords:[],
            comments:[],
            commentCnt:0,
            auctions:[],
            verify:{},
            records:[],
            foots:[],
            headImg:'',
            time:{
                hour:0,
                min:0,
                sec:0
            },
            initData:false,
            price:0,
            realPrice:0,
            param:{
                comment:false,
                vip:false,
                recharge:false,
                type:1,
                buy:false,
                price:false
            },
            comment:{
                priceTest:'',
                content:''
            },
            recharge:{
                payType:1
            },
            isFocus:0,
            isVip:0,
            isGoods:0,
            reason:'',
            phone:''
        },
        methods:{
            focus:function () {
                var that = this;
                if (this.isFocus == 0){
                    sy.postApi('/api/user/addUserFocus',{focusType:0,id:_id},function (data) {
                        location.reload();
                        // that.isFocus = 1;
                    });
                }else{
                    sy.postApi('/api/user/delUserFocus',{focusType:0,id:_id},function (data) {
                        location.reload();
                        // that.isFocus = 0;
                    });
                }

            },
            wantBuy:function(){
                var that = this;
                that.param.buy = false;
                sy.postApi('/api/index/wannaBuy',{goodsId:_id},function (data) {

                })
            },
            toWantBuy:function(){
                var that = this;
                sy.post('/web/home/findById',{},function (data) {
                    that.param.buy = true;
                    that.phone = data.data.account;
                })
            },
            jump:function (item) {
                location.href = sy.path+'/web/auction/'+item.id;
            },
            addOrder:function () {  //一口价购买
                if (this.isAuction == 2){
                    sy.msg("拍卖还没开始，请耐心等待");
                    return;
                }
                location.href = sy.path+'/web/home/makeOrder/'+this.auction.id+'?type=1';
            },
            pay:function () {
                var that = this;
                var url;
                if (that.param.type == 1){
                    //充值会员
                    url = '/web/pay/vip';
                }else {
                    //保证金
                    url = '/web/pay/cash';
                }
                sy.post(url,{
                    auctionId:that.auction.id,
                    payType:that.recharge.payType,
                    frontUrl:location.href
                },function (data) {
                    if (data.errorCode){
                        sy.msg(data.errorMsg);
                        return;
                    }
                    data = data.data;
                    if (that.recharge.payType == 1){
                        //支付宝
                        $("#content").val(data.pay);
                        $("#form").submit();
                    }else{
                        //微信二维码
                        var url = sy.path + '/qr?msg='+data.qr;
                        sy.showWxQrPay(data.price,url,function(){window.location.reload();},null);
                    }
                })
            },
            rechargeVip:function () {
                //充值保证金
                this.showRecharge(true);
                this.param.type = 1;
            },
            rechargeCash:function () {
                //充值保证金
                this.showRecharge(true);
                this.param.type = 2;
            },
            setReachargePayType:function (payType) {
                this.recharge.payType = payType;
            },
            showRecharge:function (v) {
                this.param.recharge = v;
            },
            showVip:function (v) {
                this.param.vip = v;
            },
            showComment:function (v) {
                this.param.comment = v;
            },
            addComment:function () {
              if (!this.comment.priceTest && !this.comment.content){
                  sy.msg('至少输入一项');
                  return;
              }
              var that = this;
              sy.postApi('/api/user/sendgoodsComm',{
                  id:_id,
                  content:this.comment.content,
                  priceTest:this.comment.priceTest
              },function (data) {
                  that.param.comment = false;
                  that.comment = {
                      priceTest:'',
                      content:''
                  };
                  that.loadComment();
              })
            },
            addPrice:function () {
                if (this.endMoney > 0 && this.price > this.endMoney){
                    return;
                }
                this.price += this.auction.money;
                this.realPrice = this.price;
                if (this.endMoney > 0 && this.price > this.endMoney){
                    this.price = this.endMoney;
                }
            },
            subPrice:function () {
              if (this.price > this.auction.nowPrice){
                  var price = this.realPrice - this.auction.money;
                  this.realPrice = price;
                  if (price < this.auction.nowPrice){
                      price = this.auction.nowPrice;
                  }
                  this.price = price;
              }
            },
            joinAuction:function () {
                var that = this;
                that.param.price = false;
                if (that.isAuction == 2){
                    sy.msg("拍卖还没开始，请耐心等待");
                    return;
                }
                if (that.price <= that.auction.nowPrice){
                    sy.msg('出价必须高于当前价');
                    return;
                }
                if ((that.price - that.auction.nowPrice) < that.auction.money){
                    sy.msg('每次加价不得低于'+that.auction.money);
                    return;
                }
                // if (that.price > that.auction.endMoney){
                //     sy.msg('出价不能高于一口价');
                //     return;
                // }
                sy.post('/api/pawn/joinPawn',{
                    pawnId:that.auction.id,
                    money:that.price,
                    token:_token
                },function (data) {
                    if (data.errorCode == 20042){
                        that.showVip(true);
                        return;
                    }else if (data.errorCode == 102){
                        //登录失效
                        location.href = sy.path+'/web/login?back_url='+encodeURI(location.href);
                        return;
                    }else if (data.errorCode == 200){
                        sy.msg('出价成功!');
                        that.loadRecords();
                        that.loadData();
                    }else{
                        sy.msg(data.errorMsg);
                    }

                })

            },
            loadData:function () {
                var that = this;
                sy.post('/web/art/detail',{
                    id:_id
                },function(data){
                    if (data.code == 0){
                        data = data.data;
                        var goods = data.goods;
                        goods.goodsInfo = sy.stringToJson(goods.goodsInfo);
                        $.each(goods.goodsInfo,function (i,o) {
                            o.img = o.img.split(',');
                        });
                        that.item = goods;
                        if (data.auctionType){
                            that.isAuction = data.auctionType;
                        }

                        if (that.isAuction == 1){
                            var sec = data.seconds;
                            that.calTime(sec);
                        }
                        if (data.auction){
                            that.auction = data.auction;
                            that.price = data.auction.nowPrice;
                            if (!that.auction.money){
                                that.auction.money=0;
                            }
                            that.price += that.auction.money;
                        }
                        if (goods.goodsImgs){
                            that.imgs = goods.goodsImgs.split(',');
                        }
                        if (goods.goodsTarNameList){
                            that.keywords = goods.goodsTarNameList.split(',');
                        }

                        Vue.nextTick(function () {
                            load();
                            largePhoto();
                        });
                    }else if (data.code == -2){
                        var goods = data.data;
                        goods.goodsInfo = sy.stringToJson(goods.goodsInfo);
                        $.each(goods.goodsInfo,function (i,o) {
                            o.img = o.img.split(',');
                        });
                        that.item = goods;
                        if (goods.goodsImgs){
                            that.imgs = goods.goodsImgs.split(',');
                        }
                        if (goods.goodsTarNameList){
                            that.keywords = goods.goodsTarNameList.split(',');
                        }
                        that.headImg = goods.authorHeadimg;
                        that.verifyAuth = 1;
                        Vue.nextTick(function () {
                            load()
                        });
                    }else{
                        sy.msg(data.msg);
                    }
                })
            },
            calTime:function (sec) {
                var that = this;
                var arr = DateUtil.formatSecondsToArray(sec);
                this.time.hour = arr[0];
                this.time.min = arr[1];
                this.time.sec = arr[2];
                setTimeout(function(){
                    that.calTime(--sec);
                },1000)
            },
            loadRecords:function () {       //出价记录
              var that = this;
              sy.postApi('/api/index/goodsDetail',{id:_id},function (data) {
                    that.records = data.pawnRecordViewList;
                    that.headImg = data.artHeadImg;
                    that.foots = data.goodsRecordViewList;
                    that.isFocus = data.isFocus;
                    that.isGoods = data.isVerify;
                    that.notVerifyReason = data.notVerifyReason;
                  Vue.nextTick(function () {
                      $('.offer_list').niceScroll({
                          cursorcolor: "#ccc",
                          cursoropacitymax: 1,
                          touchbehavior: false,
                          cursorwidth: "5px",
                          cursorborder: "0", //
                          cursorborderradius: "5px",
                          autohidemode: true
                      });
                  });
              })
            },
            loadComment:function () {
                var that = this;
                sy.post('/web/auction/comments',{id:_id,page:1,limit:limit},function (data) {
                    if (data.code == 0){
                        var result = data.data;
                        $(".tcdPageCode").createPage({
                            pageCount:result.totalPage,
                            current:result.currentPage,
                            backFn:function(p){
                                that.loadCommentByPage(p);
                            }
                        });
                        that.commentCnt = result.total;
                        that.comments = result.data;
                    }
                });
            },
            loadCommentByPage:function (page) {
                var that = this;
                sy.post('/web/auction/comments',{id:_id,page:page,limit:limit},function (data) {
                    if (data.code == 0){
                        var result = data.data;
                    }
                    that.comments = result.data;
                });
            },
            loadLike:function () {
                var that = this;
                sy.postApi('/api/index/goodsMore',{
                    token:_token,
                    id:_id
                },function (data) {
                    that.auctions = data;
                })
            },
            loadUserInfo:function () {
                var that = this;
                sy.post('/web/login/findById',{},function (data) {
                    if(data.code == 0){
                        data = data.data;
                        that.isVip = data.isVip;
                    }
                })
            }
        },
        created:function () {
            this.loadData();
            this.loadComment();
            this.loadLike();
            this.loadRecords();
            this.loadUserInfo();
        },
        mounted:function () {
            $('.offer_list').niceScroll({
                cursorcolor: "#ccc",
                cursoropacitymax: 1,
                touchbehavior: false,
                cursorwidth: "5px",
                cursorborder: "0", //
                cursorborderradius: "5px",
                autohidemode: true
            });


        },
        updated:function () {

        }
    });
})
var init = false;
function load() {
    if (!init){
        $('.detail_slide').flexslider({
            directionNav: true,
            pauseOnAction: false,
            controlNav:false
        });
        $('.hizoom').hiZoom({
            width: 520,
            height:350,
            position: 'right'
        });
        //init = true;
    }

}

function largePhoto() {
    var windowWidth=$(window).width();
    var windowHeight=$(window).height();
    var windowPercent=windowWidth/windowHeight;
    $('#imgshow figure').each(function(index,item){
        var figureItem=$(item).find('a');
        var figureImg=$(item).find('img');
        var figureImgWidth=figureImg.width();
        var figureImgHeight=figureImg.height();

        var figureImgPercent=figureImgWidth/figureImgHeight;

        // var thisImgWidth=windowWidth*0.8;
        // var thisImgHeight=(figureImgHeight*thisImgWidth)/figureImgWidth;
        // figureItem.attr('data-size',thisImgWidth+'x'+thisImgHeight);
        console.log(figureImgWidth,figureImgHeight)

        if(windowPercent<=figureImgPercent){
            var thisImgWidth=windowWidth*.8;
            var thisImgHeight=figureImgHeight*thisImgWidth/figureImgWidth;
            figureItem.attr('data-size',thisImgWidth+'x'+thisImgHeight)
        }else{
            var thisImgHeight=windowHeight*.8;
            var thisImgWidth=thisImgHeight*figureImgWidth/figureImgHeight;
            console.log(thisImgWidth,thisImgHeight)
            figureItem.attr('data-size',thisImgWidth+'x'+thisImgHeight)
        }

    })

    // 2018-07-19
    initPhotoSwipeFromDOM('#imgshow');
}